package com.rtx.edi.platform.mapping.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.api.annotation.RtxActivegatway;
import com.rtx.api.annotation.RtxDynamicsClass;
import com.rtx.api.annotation.RtxSTDRequest;
import com.rtx.api.enums.DynaicsClassType;
import com.rtx.common.ClassDescJsonStr;
import com.rtx.common.RtxLoadClass;
import com.rtx.edi.model.api.dto.PlApiMappingDTO;
import com.rtx.edi.model.api.dto.PlApiMappingEditDTO;
import com.rtx.edi.model.api.po.PlApiMapping;
import com.rtx.edi.model.apibaseinfo.po.PlApibaseinfo;
import com.rtx.edi.model.express.po.PlExpressApiConfig;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.baseinfo.service.PlApiService;
import com.rtx.edi.platform.baseinfo.service.PlApibaseinfoService;
import com.rtx.edi.platform.express.config.service.PlExpressApiConfigService;
import com.rtx.edi.platform.mapping.service.PlApiMappingService;
import com.rtx.wms.std.ctclass.ClassHolder;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 对接场景信息表 前端控制器
 * </p>
 *
 * @author cafebabe
 * @since 2023-03-10
 */
@Api(tags = {"对接场景管理"})
@RestController
@RequestMapping("/v1/edi/PlApiMapping")
public class PlApiMappingController {
	
	Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	PlApiMappingService PlApiMappingService; 
	
	@Autowired
	PlExpressApiConfigService plExpressApiConfigService;
	
	@Autowired
	PlApibaseinfoService plApibaseinfoService;
	
	@ApiOperation(value = "获取实现API", notes = "获取实现API")
	@PostMapping("selectActiveGatay")
	public EdiResponse querySTD(){
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		Map<String,String> std = new HashMap<String, String>();
		std.put("lable", "");
		std.put("value", "");
		list.add(std);
		
		Set<Class<?>> set = RtxLoadClass.getClassSet("com.rtx.edi.platform.synctask.service.impl");
		
		for(Class clazz:set) {
			RtxActivegatway rtxActivegatway = (RtxActivegatway) clazz.getDeclaredAnnotation(RtxActivegatway.class);
			Service service = (Service) clazz.getDeclaredAnnotation(Service.class);
			if(null != rtxActivegatway) {
				
				Map<String,String> s = new HashMap<String, String>();
				s.put("label", rtxActivegatway.value());
				s.put("value", service.value());
				list.add(s);
			}
			
		}
		
		return EdiResponse.success().data("item", list);
	}
	
	@ApiOperation(value = "获取测试源api", notes = "获取测试源api")
	@PostMapping("selectRequestSourceApi")
	public EdiResponse requestSourceApi(@RequestBody(required = false) String request) {
		JSONObject req = JSON.parseObject(request);
		Long id = Long.parseLong(req.getString("id"));
		PlApiMapping mapping = PlApiMappingService.getById(id);
		
		PlApibaseinfo sapi = plApibaseinfoService.getById(mapping.getSourceApiId());
		return EdiResponse.success().data("item", sapi);
	}
	
	
	@ApiOperation(value = "测试api", notes = "测试api")
	@PostMapping("sendTestRequest")
	public EdiResponse sendTestRequest(@RequestBody(required = false) String request) {
		String response = PlApiMappingService. sendTestRequest(request);
		return EdiResponse.success().data("item", response);
	}
	
	@ApiOperation(value = "获取快递接口配置下拉框", notes = "获取快递接口配置下拉框")
	@PostMapping("selectExpressConfigSelect")
	public EdiResponse selectExpressConfig(@RequestBody(required = false) String request) {
		List<PlExpressApiConfig> dataList = plExpressApiConfigService.selectList("ris_edi", Wrappers.lambdaQuery());
		
		List<Map<String,Object>> resList = new ArrayList<>();
		Map<String,Object> map = new HashMap<>();
		map.put("label", "");
		map.put("label", "");
		for(PlExpressApiConfig config:dataList) {
			Map<String,Object> s = new HashMap<>();
			s.put("label", config.getConfigname());
			s.put("value", config.getId());
			resList.add(s);
		}
		return EdiResponse.success().data("item", resList);
	}
	
	/**
	 * 获取报文映射列表
	 * @return
	 */
	@ApiOperation(value = "编辑_对接场景", notes = "编辑_对接场景")
	@PostMapping("selectPlApiMappingSelect")
	public EdiResponse selectPlApiMappingSelect(@RequestBody(required = false) PlApiMappingEditDTO dto) {
		return EdiResponse.success().data("item", PlApiMappingService.selectPlApiMappingSelect());
	}
	
	/**
	 * 获取报文映射列表
	 * @return
	 */
	@ApiOperation(value = "编辑_对接场景", notes = "编辑_对接场景")
	@PostMapping("editPlApiMapping")
	public EdiResponse editPlApiMappingStructure(@RequestBody(required = false) PlApiMappingEditDTO dto) {
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		String companyid = (String) Optional.ofNullable(session.getAttribute("companyid")).orElse("1");
		dto.setCompanyId(Long.parseLong(companyid));

		PlApiMappingService.editPlApiMappingStructure(dto);
		return EdiResponse.success();
	}
	
	/**
	 * 获取对接场景列表
	 * @return
	 */
	@ApiOperation(value = "对接场景列表", notes = "分页获取对接场景列表")
	@PostMapping("selectPlApiMappingList")
	public EdiResponse selectPlApiMappingList(@RequestBody(required = false) PlApiMappingDTO dto){
		Page<PlApiMapping> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
		PlApiMapping po = new PlApiMapping();
		BeanUtils.copyProperties(dto, po);
		PlApiMappingService.selectPlApiMappingList(pageParam,po);
		return EdiResponse.success().data("page",pageParam);
	}
	

	/**
	 * 根据ID获取对接场景
	 * @param PlApiMapping
	 * @return
	 */
	@ApiOperation(value = "查询对接场景", notes = "根据ID查询对接场景")
	@PostMapping("selectPlApiMappingById")
	public EdiResponse selectPlApiMappingById(@RequestBody PlApiMappingDTO dto){
		return EdiResponse.success().data("item",PlApiMappingService.getById(dto.getId()));
	}
	/**
	 * 根据id删除
	 * @param PlApiMapping
	 * @return
	 */
	@ApiOperation(value = "删除对接场景", notes = "根据id删除对接场景", httpMethod = "POST")
	@PostMapping("deletePlApiMappingById")
	public EdiResponse deletePlApiMappingById(@RequestBody PlApiMappingDTO dto) {
		return PlApiMappingService.removeById(dto.getId()) ? EdiResponse.success() : EdiResponse.failed();
	}
	
    /**
     * 根据id列表删除
     * @param list
     * @return
     */
    @ApiOperation(value = "删除对接场景_批量", notes = "根据id列表删除对接场景", httpMethod = "POST")
    @PostMapping("deletePlApiMappingByIds")
    public EdiResponse deletePlApplicationByIds(@RequestBody List<PlApiMappingDTO> list) {
    	try {
    		PlApiMappingService.deletePlApiMappingByIds(list);
    		return EdiResponse.success();
    	} catch (Exception e) {
    		return EdiResponse.failed().returnMgr(e.getMessage());
    	}
    }

	/**
	 * 新增对接场景
	 * @param PlApiMapping
	 * @return
	 */
	@ApiOperation(value = "新增对接场景")
	@PostMapping("addPlApiMapping")
	public EdiResponse addPlApiMapping(@RequestBody PlApiMappingDTO dto){
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		String companyid =  (String) Optional.ofNullable(session.getAttribute("companyid")).orElse("1");
		
		dto.setCompanyId(Long.parseLong(companyid));
		
		PlApiMappingService.addApiMapping(dto);
		return EdiResponse.success();
	}

	/**
	 * 根据ID修改对接场景
	 * @return
	 */
	@ApiOperation(value = "修改对接场景", notes = "根据ID修改对接场景")
	@PostMapping("editPlApiMappingById")
	public EdiResponse editPlApiMappingById(@RequestBody PlApiMappingDTO dto){
		PlApiMapping po = new PlApiMapping();
		BeanUtils.copyProperties(dto, po);
		return PlApiMappingService.updateById(po) ? EdiResponse.success() : EdiResponse.failed();
	}
}
